﻿/*   
  
   g10EXETryItCS -               Copyright (C) 2011 PH Informática, SA.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the  Free Software Foundation, either version 3 of the  License,  or
   (at your option) any later version.

   This  program  is distributed in  the hope that it will  be  useful,
   but  WITHOUT  ANY  WARRANTY; without even the  implied  warranty  of
   MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.  See  the
   GNU General Public License for more details.

   You  should have received  a copy of  the GNU General Public License
   along with this program. If not, see <http://www.gnu.org/licenses/>.

*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace g10EXETryItCS
{
   
    public partial class frmMapa : Form
    {

    // Flag de PAN
    private bool onDrag = false;

    // Objectos de auxílio a "movimentações" da imagem do mapa
    private double dMapCenterX = -35000;
    private double dMapCenterY = 160000;
    private long lMapScale = 100000;

    private Bitmap oAuxBitmap;
    private g10WSTryIt.g10GeoPoint oPonto;
        
        public frmMapa()
        {
            InitializeComponent();
        }

        private void frmMapa_Load(object sender, EventArgs e)
        {
            ToolStripStatusLabel1.Text = "A carregar mapa inicial ...";
            Application.DoEvents();

            // obter mapa
            System.Drawing.Image oImage;
            oImage = csModule.GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height);


            if(!(oImage==null)) {
                PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                PictureBox1.Image = oImage;
            }

            ToolStripStatusLabel1.Text = "A carregar lista de layers ...";
            Application.DoEvents();

            g10WSTryIt.g10MapLayerSet oLayers = new g10WSTryIt.g10MapLayerSet();
            oLayers = csModule.myWs.GetLayers(csModule.sApiTokenID, csModule.sSessionID);

           csModule.bLoading = true;

            // carregar Layers
            TreeView1.Nodes.Clear();
            if(!(oLayers==null)) {
                for(int i=0;i<oLayers.Layers.Count();i++) {
                    g10WSTryIt.g10MapguideLayer l = oLayers.Layers[i];
                    TreeView1.Nodes.Add(l.Name, l.LegendLabel).Checked = l.GetVisibility;
                }
            }

            // apenas concelhos do distrito do porto
            LoadConcelhos("cc0d86be-352a-43e8-b77b-9d370c079471");

            csModule.bLoading = false;

            ToolStripStatusLabel1.Text = "OK!";
            Application.DoEvents();
        }

        private void PictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            if (!onDrag)
            {
                onDrag = true;
                oAuxBitmap = new Bitmap(PictureBox1.Image);

                oPonto = new g10WSTryIt.g10GeoPoint();
                oPonto.X = e.X;
                oPonto.Y = e.Y;

            }

        }


        private void PictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            if (onDrag)
            {
                double midX = ((PictureBox)sender).Width / 2 + (oPonto.X - e.X);
                double midY = ((PictureBox)sender).Height / 2 + (oPonto.Y - e.Y);

                int newHW = Math.Max(((PictureBox)sender).Width, ((PictureBox)sender).Height);

                Graphics g = Graphics.FromImage(PictureBox1.Image);
                g.Clear(Color.Beige);

                Rectangle destRect = new Rectangle(0, 0, ((PictureBox)sender).Width, ((PictureBox)sender).Height);
                Rectangle srcRect = new Rectangle((int)(midX - newHW / 2), (int)(midY - newHW / 2), newHW, newHW);

                g.DrawImage(oAuxBitmap, destRect, srcRect, GraphicsUnit.Pixel);
                PictureBox1.Invalidate();

                Application.DoEvents();

            }

        }


        private void PictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            if (onDrag)
            {
                onDrag = false;

                g10WSTryIt.g10GeoPoint pci = new g10WSTryIt.g10GeoPoint();
                pci.X = ((PictureBox)sender).Width / 2 - (e.X - oPonto.X);
                pci.Y = ((PictureBox)sender).Height / 2 - (e.Y - oPonto.Y);

                g10WSTryIt.g10GeoPoint pWorld = new g10WSTryIt.g10GeoPoint();
                pWorld = csModule.myWs.GetActualViewCoordsImage2Map(csModule.sApiTokenID, csModule.sSessionID, pci);

                dMapCenterX = pWorld.X;
                dMapCenterY = pWorld.Y;

                ToolStripStatusLabel1.Text = "A obter mapa ...";
                Application.DoEvents();

                PictureBox1.Image = csModule.GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height);

                ToolStripStatusLabel1.Text = "";
                Application.DoEvents();

            }

        }


        private void TreeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (!csModule.bLoading)
            {
                csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, e.Node.Name, e.Node.Checked);

                ToolStripStatusLabel1.Text = "A obter mapa ...";
                Application.DoEvents();

                PictureBox1.Image = csModule.GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height);

                ToolStripStatusLabel1.Text = "";
                Application.DoEvents();
            }

        }


        private void LoadConcelhos(string distritoid)
        {
            ToolStripStatusLabel1.Text = "A carregar lista de concelhos ...";
            Application.DoEvents();

            ListBox2.Items.Clear();
            ListBox3.Items.Clear();

            g10WSTryIt.Entidades concelhos = default(g10WSTryIt.Entidades);
            concelhos = csModule.myWs.getConcelhos(csModule.sApiTokenID, csModule.sSessionID, distritoid);

            if (concelhos != null)
            {
                if (concelhos.Entidades1 != null)
                {
                    foreach (g10WSTryIt.Entidade c in concelhos.Entidades1)
                    {
                        ListBox2.Items.Add(new csModule.ListItem(c.Nome, c.Id));
                    }
                }
            }

            ToolStripStatusLabel1.Text = "";
            Application.DoEvents();

        }


        private void LoadFreguesias(string concelhoid)
        {
            ToolStripStatusLabel1.Text = "A carregar lista de freguesias ...";
            Application.DoEvents();

            ListBox3.Items.Clear();

            g10WSTryIt.Entidades freguesias = default(g10WSTryIt.Entidades);
            freguesias = csModule.myWs.getFreguesias(csModule.sApiTokenID, csModule.sSessionID, concelhoid);

            if (freguesias != null)
            {
                if (freguesias.Entidades1 != null)
                {
                    foreach (g10WSTryIt.Entidade f in freguesias.Entidades1)
                    {
                        ListBox3.Items.Add(new csModule.ListItem(f.Nome, f.Id));
                    }
                }
            }

            ToolStripStatusLabel1.Text = "";
            Application.DoEvents();

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            ToolStripStatusLabel1.Text = "A obter mapa ...";
            Application.DoEvents();

            // Zoom menos
            lMapScale *= 2;
            PictureBox1.Image = csModule.GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height);

            ToolStripStatusLabel1.Text = "";
            Application.DoEvents();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            ToolStripStatusLabel1.Text = "A obter mapa ...";
            Application.DoEvents();

            // Zoom mais
            lMapScale = (int)(lMapScale / 2.0);
            PictureBox1.Image = csModule.GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height);

            ToolStripStatusLabel1.Text = "";
            Application.DoEvents();
        }

        private void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ToolStripStatusLabel1.Text = "A localizar concelho ...";
            Application.DoEvents();

            string idConc = ((csModule.ListItem)ListBox2.SelectedItem).Key();

            LoadFreguesias(idConc);

            g10WSTryIt.g10GeoPoint cent = default(g10WSTryIt.g10GeoPoint);
            cent = csModule.myWs.getGeometryCentroidById(csModule.sApiTokenID, csModule.sSessionID, idConc);

            csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, "freguesias", false);
            csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, "concelhos", true);
            csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, "distritos", false);

            lMapScale = 200000;

            ToolStripStatusLabel1.Text = "A obter mapa ...";
            Application.DoEvents();

            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            PictureBox1.Image = csModule.GetMapa(cent.X, cent.Y, lMapScale, PictureBox1.Width, PictureBox1.Height);

            dMapCenterX = cent.X;
            dMapCenterY = cent.Y;

            ToolStripStatusLabel1.Text = "";
            Application.DoEvents();

        }

        private void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ToolStripStatusLabel1.Text = "A localizar freguesia ...";
            Application.DoEvents();

            string idFreg = ((csModule.ListItem)ListBox3.SelectedItem).Key();

            g10WSTryIt.g10GeoPoint cent = default(g10WSTryIt.g10GeoPoint);
            cent = csModule.myWs.getGeometryCentroidById(csModule.sApiTokenID, csModule.sSessionID, idFreg);

            csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, "freguesias", true);
            csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, "concelhos", false);
            csModule.myWs.SetLayerVisibility(csModule.sApiTokenID, csModule.sSessionID, "distritos", false);

            lMapScale = 50000;

            ToolStripStatusLabel1.Text = "A obter mapa ...";
            Application.DoEvents();

            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            PictureBox1.Image = csModule.GetMapa(cent.X, cent.Y, lMapScale, PictureBox1.Width, PictureBox1.Height);

            dMapCenterX = cent.X;
            dMapCenterY = cent.Y;

            ToolStripStatusLabel1.Text = "";
            Application.DoEvents();
        }

    }
}
